﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;

/// <summary>
/// 用于动态创建类的构建器。
/// </summary>
public sealed class ClassBuilder : TypeBuilder {

    /// <summary>
    /// 获取或设置当前类的基类。
    /// </summary>
    public TypeBuilder baseType;

    /// <summary>
    /// 获取或设置当前类实现的接口。
    /// </summary>
    public SingleLinkedList<TypeBuilder> implementedTypes = new SingleLinkedList<TypeBuilder>();

    public override void write(WriteContext context) {

        attributes |= MemberAttributes.@class;

        int implementedTypeCount = implementedTypes.length;
        if (implementedTypeCount > 0) {
            attributes |= MemberAttributes.hasImplementedType;
        }

        base.write(context);

        Debug.Assert(baseType != null);
        context.writeMember(baseType);

        if (implementedTypeCount > 0) {
            Debug.Assert(implementedTypeCount <= byte.MaxValue);
            context.writeByte((byte)implementedTypeCount);
            foreach (var implmementedType in implementedTypes) {
                context.writeMember(implmementedType);
            }
        }

    }

}
